import uvicorn
from fastapi import FastAPI, Body
import base64
import ddddocr



app = FastAPI(title='StupidOCR - 开发者文档', description='代码编写：81NewArk',version="1.0.2")


@app.post("/api.Image_to_Text", summary='识别图片内文字', description='普通图片验证码识别，上传图片的Base64编码', tags=['图片验证码识别'])
def OcrImageBase64(ImageBase64: str = Body(..., title='验证码图片Bse64文本', embed=True)):

    base64_data = base64.b64decode(ImageBase64)
    ocr = ddddocr.DdddOcr(show_ad=False)
    res = ocr.classification(base64_data)
    return {"result": res,
            "Blog":"https://Niuer-Pepsi.lofter.com"}


@app.post("/api.Image_to_Arithmetic", summary='识别算数验证码', description='算术题验证码识别，上传图片的Base64编码，提供两个返回，自行取值分割文本并识别',tags=['图片验证码识别'])
def OcrImageBase64(ImageBase64: str = Body(..., title='验证码图片Bse64文本', embed=True)):

    base64_data = base64.b64decode(ImageBase64)
    ocr = ddddocr.DdddOcr(show_ad=False)
    res = ocr.classification(base64_data)

    if "+" or '-' or 'x' or '/' not in res:
        zhi="Calculation error"
    if '+' in res:
        zhi = int(res.split('+')[0]) + int(res.split('+')[1][:-1])
    if '-' in res:
        zhi = int(res.split('-')[0]) - int(res.split('-')[1][:-1])
    if 'x' in res:
        zhi = int(res.split('x')[0]) * int(res.split('x')[1][:-1])
    if '/' in res:
        zhi = int(res.split('/')[0]) / int(res.split('/')[1][:-1])


    return {"solution_result": zhi,
            "raw_result":res,
            "Blog":"https://Niuer-Pepsi.lofter.com"
            }


@app.post("http://Niuer-Pepsi.lofter.com", summary='欢迎关注作者的乐乎博客', description='求一键三连，我想混个乐乎认证',tags=['作者博客：Niuer-Pepsi.lofter.com'])
def Niuer():
    return{"Blog":"Niuer-Pepsi.lofter.com"}


if __name__ == '__main__':
    print("")
    print("")
    print("   _____   _                             _        ____     _____   _____  ")
    print("  / ____| | |                   (_)     | |      / __ \   / ____| |  __ \ ")
    print(" | (___   | |_   _   _   _ __    _    __| |     | |  | | | |      | |__) |")
    print("  \___ \  | __| | | | | | '_ \  | |  / _` |     | |  | | | |      |  _  / ")
    print("  ____) | | |_  | |_| | | |_) | | | | (_| |  _  | |__| | | |____  | | \ \ ")
    print(" |_____/   \__|  \__,_| | .__/  |_|  \__,_| (_)  \____/   \_____| |_|  \_/")
    print("                        | |                                               ")
    print("                        |_|                                               ")
    print("")
    print("")
    print("                 开发者文档：http://127.0.0.1:6688/docs          ")
    print("                 项目依赖：FastAPI+ Uvicorn + DDDDOCR            ")
    print("")
    print("                 乐乎博客：https://Niuer-Pepsi.lofter.com        ")
    print("                 代码编写：81NewArk                              ")
    print("")
    print("")
    print("")
    print("")

    uvicorn.run(app, port=6688, host="127.0.0.1")



